package zhou.chs.p2_4;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import java.util.Timer;
import java.util.TimerTask;
import static java.lang.Math.sin;

public class MainActivity extends AppCompatActivity {
    private Bitmap bitmap;      //图形文件格式
    private Canvas canvas;      //画布
    private Paint paint;        //画笔
    ImageView img;                //声明ImageView
    public int dat[] = new int[100];        //定义整数数组，存放曲线数据
    private Handler myhandler;              //声明线程myhandler
    int tn;     //正弦波初相位值，不断变化出现移动效果
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        img = (ImageView) findViewById(R.id.idVw);
        myhandler = new MyHandler();       //新建Handler，用于线程间的通信
        Timer mTimer = new Timer();         //新建Timer
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                tn++;                   //每秒加1
                Message msg1 = myhandler.obtainMessage();   //创建消息
                msg1.what = 1;                      //变量what赋值
                myhandler.sendMessage(msg1);     //发送消息
            }
        }, 100, 100);     //延时100ms，然后每隔100ms发送消息
    }
    //处理接收到的消息
    class MyHandler extends Handler {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //给曲线赋值，50Hz正弦波数据
                    for(int t=0;t<100;t++) dat[t]=(int)(100*sin(2*3.14*50*(t+tn)/1000));
                    Show();   //显示曲线
                    break;
            }
        }
    }
    //显示曲线
    public void Show() {
        int mW=img.getWidth();     //ImageView对象的宽
        int mH=img.getHeight();    //ImageView对象的高
        if (bitmap == null) {                //创建一个新的bitmap对象，宽、高使用界面布局中ImageView对象的宽、高
            bitmap = Bitmap.createBitmap(mW, mH, Bitmap.Config.RGB_565);
        }
        canvas = new Canvas(bitmap);        //根据bitmap对象创建一个画布
        canvas.drawColor(Color.WHITE);      //设置画布背景色为白色
        paint = new Paint();                //创建一个画笔对象
        paint.setStrokeWidth(8);             //设置画笔的线条粗细为8磅
        paint.setColor(Color.BLACK);         //画笔颜色为黑色
        canvas.drawLine(0, 0, mW, 0, paint);  //画外框
        canvas.drawLine(0, mH, mW, mH, paint);
        canvas.drawLine(0, 0, 0, mH, paint);
        canvas.drawLine(mW, 0, mW, mH, paint);
        paint.setTextSize(48);          //字符大小为48
        canvas.drawText("移动的正弦波",200,100,paint);    //画字符
        paint.setStrokeWidth(2);            //设置画笔的线条粗细为2磅
        paint.setColor(Color.RED);            //画笔颜色改为红色
        for (char i = 1; i < 100; i++) {    //画曲线
            canvas.drawLine((i - 1)*mW/100, mH/2+dat[i - 1], i*mW/100, mH/2+dat[i], paint);
        }
        img.setImageBitmap(bitmap);     //在ImageView中显示bitmap
    }
}
